/*
 * easysmtp - A simple C lib in Linux to help you send mails in an easy way
 * Copyright (C) 2012-2013, easysmtp developers and contributors
 *
 * easysmtp is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * easysmtp is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with easysmtp; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef _SEND_MAIL_H_
#define _SEND_MAIL_H_

#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/bio.h>

#include "change64.h"
#include "error.h"

#define SYS_ERROR -1
#define SMTP_ERROR -2
#define SSL_ERROR -3
#define SMTP_SUCCESS 0

#define	TEXT_MAXLEN 100
#define SMTP_MAXLEN 256
#define ANSWER_MAXLEN 512

#define AUTH_LOGIN_SIZE 8 
#define AUTH_PLAIN_SIZE 9

#define CERTF "client.crt"
#define KEYF "client.key"
#define CACERT "ca.crt"
#define PASSWD "passwd"	 
#define SMTPPORT 25
#define SSLPORT 465

/*
 * 定义一个用于smtp发送邮件的类型
 */
typedef struct Smtp {
	char server_addr[SMTP_MAXLEN+1];
	char domain[SMTP_MAXLEN+1];
	char username[SMTP_MAXLEN+1];
	char passwd[SMTP_MAXLEN+1];
	char buf[ANSWER_MAXLEN+1];
	char **recv_list;
	int recv_len;
	char **cc_list;
	int cc_len;
	char **bcc_list;
	int bcc_len;
	struct timeval *connect_tv; //用于非阻塞连接超时的结构
	struct timeval *recv_tv; //用于接受超时的结构
	int sockfd;
	int domain_flag;
	int method_flag;
	int ssl_flag;
	SSL *ssl;
}SMTP;

/*
 * 定义一个邮件类型，包括有标题，内容
 */
typedef struct mail {
	char title[TEXT_MAXLEN+1];
	char *text_plain;
	int text_plain_len;
	char *text_html;
	int text_html_len;
	char content_type[SMTP_MAXLEN+1];
	char *html_content;
	char **file_path_list; //附件路径
	char **file_name_list;
	int att_len;
}MAIL;

extern SMTP* smtp_init(void);
extern MAIL* mail_init(void);
extern int set_server_addr(SMTP *smtpr, const char *server_addr);
extern int set_domain(SMTP *smtpr, const char *domain);
extern int set_auth_method(SMTP *smtpr, const char *method);
extern int set_username(SMTP *smtpr, const char *username);
extern int set_passwd(SMTP *smtpr, const char *passwd);
extern int set_recv_list(SMTP *smtpr, char recv_list[][SMTP_MAXLEN+1], 
				int len);
extern int add_one_recv(SMTP *smtpr, const char *recv);
extern int set_cc_list(SMTP *smtpr, char cc_list[][SMTP_MAXLEN+1], 
				int len);
extern int add_one_cc(SMTP *smtpr, const char *cc);
extern int set_bcc_list(SMTP *smtpr, char bcc_list[][SMTP_MAXLEN+1],
				int len);
extern int add_one_bcc(SMTP *smtpr, const char *bcc);
extern int set_connect_tv(SMTP *smtpr, int sec);
extern int set_recv_tv(SMTP *smtpr, int sec);
extern int set_title(MAIL *mtpr, const char *title);
extern int set_content(MAIL *mtpr, const char *content, 
					const char *content_type);
extern int set_attachment_list(MAIL *mpr,  
			char file_path_list[][SMTP_MAXLEN+1],
			char file_name_list[][SMTP_MAXLEN+1], int len);
extern int add_one_attachment(MAIL *mpr, const char *file_path, 
			const char *file_name);
extern int mail_attachment(SMTP *smtpr, MAIL *mpr);
extern int send_msg(SMTP *smtpr, char *buf);
extern int read_msg(SMTP *smtpr);
extern int send_mail(SMTP *smtpr, MAIL *mpr);
extern int send_ssl_mail(SMTP *smtpr, MAIL *mpr);
extern void smtp_destroy(SMTP *smtpr, MAIL *mpr);

#endif
